package day01;

import java.util.concurrent.*;

public class Demo07 {
    public static void main(String[] args) {
        /**
         * 继承Thread创建线程
         */
        Thread t1 = new Thread(){
            @Override
            public void run() {
                System.out.println("T1");
            }
        };
        t1.start();
        /**
         * 实现Runnable接口
         */
        Thread t2 = new Thread(new Runnable(){
            @Override
            public void run() {
                System.out.println("T2");
            }
        });
        t2.start();
        /**
         * 利用线程池执行线程
         */
        //newFixedThreadPool 创建固定大小的线程池，这里5个线程
        ExecutorService pool = Executors.newFixedThreadPool(5);
        /**
         * execute 执行没有返回结果的线程
         */
        pool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("T3");
            }
        });
        /**
         * submit 执行有结果的线程，其中future代表线程的执行结果
         */
        Future<String> future = pool.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println("正在执行T4");
                return "T4";
            }
        });
        try {
            //等待10秒拿结果
            System.out.println("拿到结果："+future.get(10, TimeUnit.SECONDS));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
